﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Unity;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model;
using Autorent.Desktop.Model.AutorentMngrServiceReference;
using Autorent.Desktop.Model.Services;
using Autorent.Desktop.Modules.Events;
using Autorent.Desktop.Modules.Orders.Services;
using Autorent.Desktop.Modules.Orders.Views;

namespace Autorent.Desktop.Modules.Orders.ViewModels
{
    public class OrdersListViewModel : ViewModelBase
    {
        #region Enums

        public enum OrdersFilterType { ByManager, New, All }

        #endregion

        #region Public Properties

        public OrdersListBaseView View { get; set; }

        public AutoOrder SelectedAutoOrder { get; set; }

        public ObservableCollection<AutoOrder> Orders
        {
            get
            {
                ObservableCollection<AutoOrder> orders = null;

                switch (filterType)
                {
                    case OrdersFilterType.ByManager:
                        orders = _repository.GetOrdersByManager(_systemUsersRepository.GetSystemUserByName(User.UserName).SystemUser_ID);
                        break;
                    case OrdersFilterType.All:
                        orders = _repository.GetOrders();
                        break;
                    case OrdersFilterType.New:
                        orders = _repository.GetNewOrders();
                        break;
                }
                
                return orders;
            }
        }

        #endregion

        #region Constructors

        public OrdersListViewModel(OrdersFilterType filterType, IAutoOrdersRepository repository,
            ISystemUsersRepository systemUsersRepository, IUnityContainer container, IRegionManager regionManager,
            IEventAggregator eventAggregator)
        {
            View = CreateView(filterType);
            View.Model = this;

            _repository = repository;
            _systemUsersRepository = systemUsersRepository;
            _container = container;
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;

            _eventAggregator.GetEvent<CollectionChangedEvent>().Subscribe(OnPropertyChanged);
            _eventAggregator.GetEvent<ViewClosedEvent>().Subscribe(ViewClosedEventHandler);

            this.filterType = filterType;            
        }

        #endregion

        #region Commands

        public ICommand CreateAutoOrder
        {
            get
            {
                return new RelayCommand(param => createAutoOrder());
            }
        }

        public ICommand UpdateAutoOrder
        {
            get
            {
                return new RelayCommand(param => updateAutoOrder());
            }
        }

        public ICommand DeleteAutoOrder
        {
            get
            {
                return new RelayCommand(param => deleteAutoOrder());
            }
        }

        public ICommand SearchAutoOrder
        {
            get
            {
                return new RelayCommand(param => searchAutoOrder());
            }
        }

        public ICommand ApproveOrder
        {
            get
            {
                return new RelayCommand(param => approveAutoOrder());
            }
        }

        public ICommand Pay
        {
            get
            {
                return new RelayCommand(param => pay());
            }
        }

        #endregion

        #region Private variables

        private readonly IAutoOrdersRepository _repository;
        private readonly ISystemUsersRepository _systemUsersRepository;
        private readonly IEventAggregator _eventAggregator;
        private readonly IUnityContainer _container;
        private readonly IRegionManager _regionManager;

        private OrdersFilterType filterType;

        #endregion

        #region Private Methods

        private OrdersListBaseView CreateView(OrdersFilterType viewType)
        {
            OrdersListBaseView view = null;

            switch (filterType)
            {
                case OrdersFilterType.ByManager:
                    view = new MyOrdersListView();
                    break;
                case OrdersFilterType.New:
                    view = new NewOrdersListView();
                    break;
                case OrdersFilterType.All:
                    view = new AllOrdersListView();
                    break;
            }

            return view;
        }

        private void createAutoOrder()
        {
            object view = _regionManager.Regions["DetailsRegion"].GetView("CreateAutoOrderView");

            if (view == null) { _regionManager.Regions["DetailsRegion"].Add(_container.Resolve<CreateAutoOrderViewModel>().View, "CreateAutoOrderView"); }
            else { _regionManager.Regions["DetailsRegion"].Remove(view); }
        }

        private void updateAutoOrder()
        {
            if (SelectedAutoOrder != null)
            {
                object view = _regionManager.Regions["DetailsRegion"].GetView("UpdateAutoOrderView");

                if (view == null) { _regionManager.Regions["DetailsRegion"].Add(_container.Resolve<UpdateAutoOrderViewModel>(new ParameterOverrides() { { "autoOrder", SelectedAutoOrder }}).View, "UpdateAutoOrderView"); }
                else { _regionManager.Regions["DetailsRegion"].Remove(view); }
            }
            else
            {
                MessageBox.Show("Select the Order", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void deleteAutoOrder()
        {
            _repository.DeleteAutoOrder(SelectedAutoOrder);

            OnPropertyChanged("Orders");
        }

        private void searchAutoOrder()
        {
            object view = _regionManager.Regions["SearchRegion"].GetView("SearchAutoOrderView");

            if (view == null) { _regionManager.Regions["SearchRegion"].Add(_container.Resolve<SearchAutoOrderViewModel>().View, "SearchAutoOrderView"); }
            else { _regionManager.Regions["SearchRegion"].Remove(view); }
        }

        private void approveAutoOrder()
        {
            if (SelectedAutoOrder != null)
            {
                this.View.Visibility = Visibility.Collapsed;

                ApproveOrderView view = _container.Resolve<ApproveOrderViewModel>(
                    new ParameterOverrides() { { "autoOrder", SelectedAutoOrder } }).View;

                _regionManager.Regions["ApplicationContentRegion"].Add(view, "ApproveOrderView");
                _regionManager.Regions["ApplicationContentRegion"].Activate(view);
            }
            else { MessageBox.Show("Select the order!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); }
        }

        private void pay()
        {
            if (SelectedAutoOrder != null)
            {
                if (SelectedAutoOrder.Payment != null)
                {
                    this.View.Visibility = Visibility.Collapsed;

                    PayReportView view = _container.Resolve<PayReportViewModel>(
                        new ParameterOverrides() { { "autoOrder", SelectedAutoOrder } }).View;

                    _regionManager.Regions["ApplicationContentRegion"].Add(view, "ApproveOrderView");
                    _regionManager.Regions["ApplicationContentRegion"].Activate(view);
                }
                else { MessageBox.Show("Approve the order before!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); }
            }
            else { MessageBox.Show("Select the order!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); }

        }

        private void ViewClosedEventHandler(Type viewType)
        {
            if ((viewType == typeof(ApproveOrderView)) || (viewType == typeof(PayReportView)))
            {
                View.Visibility = Visibility.Visible;
                //_regionManager.Regions["ListRegion"].Activate(View);
                OnPropertyChanged("Orders");
            }
        }

        #endregion
    }
}
